﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HipChat;
using Newtonsoft.Json;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        HipChatClient client = new HipChatClient();
        public Form1()
        {
            InitializeComponent();
            //richTextBox2.Rtf = @"{\rtf1\ansi This is in \b bold\b0.}";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            client = new HipChatClient(textBox1.Text, textBox2.Text);
            string testResult = client.ListRooms();
            if(testResult.Contains("348106") && testResult.Contains("Welcome"))
            {
                button1.Text = "Ok!";
                button1.Enabled = false;
                //client.From = "ABCDEFGHIJKLMNOP";
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (client != null)
            {
                client.From = "ABCDEFGHIJKLMNOP";
                client.SendMessage(richTextBox1.Text, HipChatClient.BackgroundColor.random);
            }
            MessageBox.Show("Message Sent!");
            richTextBox1.Text = "";
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (client != null)
            {
                List<Message> list = JsonToMsg.J2M(client.RoomHistory(DateTime.Today));
                if (list != null)
                {
                    richTextBox2.Clear();
                }
                foreach (Message m in list)
                {
                    string singleMessgae = m.name["name"] + " ## " + m.message + " ((" + ProcessDateFromJson(m.date) + "))" + "\n";
                    richTextBox2.Text += singleMessgae;

                    //Font fnt = new Font("Microsoft YaHei", 10F, FontStyle.Italic, GraphicsUnit.Point);
                    //string mystring = "!!";
                    
                    //if (richTextBox2.Find(mystring) > 0)
                    //{
                    //    int my1stPosition = richTextBox2.Find(mystring);
                    //    richTextBox2.SelectionStart = my1stPosition;
                    //    richTextBox2.SelectionLength = singleMessgae.IndexOf("##");
                    //    richTextBox2.SelectionFont = fnt;
                    //    richTextBox2.SelectionColor = Color.CadetBlue;

                    //}           
                }   
            }
        }


        private DateTime ProcessDateFromJson(string jsonDate)
        {
            return DateTime.Parse(jsonDate);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if(client!=null)
            {
                List<Room> roomList = JsonToMsg.GetRoomUser(client.RoomShow());
            }
        }

    
    }
}
